#ifndef DEZ_TRANSPORT_H
#define DEZ_TRANSPORT_H
#include <stdlib.h>
#include "esp_netif.h"
#include "esp_wifi.h"
#include "esp_now.h"
#include "esp_crc.h"
#include "I2S_Audio.h"
#include "esp_log.h"
#if CONFIG_ESPNOW_WIFI_MODE_STATION
#define ESPNOW_WIFI_MODE WIFI_MODE_STA
#define ESPNOW_WIFI_IF   ESP_IF_WIFI_STA
#else
#define ESPNOW_WIFI_MODE WIFI_MODE_AP
#define ESPNOW_WIFI_IF   ESP_IF_WIFI_AP
#endif

#define ESPNOW_QUEUE_SIZE           6
#define IS_BROADCAST_ADDR(addr) (memcmp(addr, s_dez_broadcast_mac, ESP_NOW_ETH_ALEN) == 0)

extern bool sendDone;//发送完成标志位
extern bool recOver;
extern bool speaking;
extern int  AUDIT_BIT;

typedef enum {
    DEZ_ESPNOW_SEND_CB,
    DEZ_ESPNOW_RECV_CB,
} dez_espnow_event_id_t;

enum {
    DEZ_ESPNOW_DATA_BROADCAST,
    DEZ_ESPNOW_DATA_UNICAST,
    DEZ_ESPNOW_DATA_MAX,
};

typedef struct {
    uint8_t mac_addr[ESP_NOW_ETH_ALEN];
    esp_now_send_status_t status;
} dez_espnow_event_send_cb_t;

typedef struct {
    uint8_t mac_addr[ESP_NOW_ETH_ALEN];
    uint8_t *data;
    int data_len;
} dez_espnow_event_recv_cb_t;

typedef union {
    dez_espnow_event_send_cb_t send_cb;
    dez_espnow_event_recv_cb_t recv_cb;
} dez_espnow_event_info_t;

/* When ESPNOW sending or receiving callback function is called, post event to ESPNOW task. */
typedef struct {
    dez_espnow_event_id_t id;
    dez_espnow_event_info_t info;
} dez_espnow_event_t;

/* User defined field of ESPNOW data . */
typedef struct {
    uint8_t type;                         //Broadcast or unicast ESPNOW data.
    uint8_t state;                        //Indicate that if has received broadcast ESPNOW data or not.
    uint16_t seq_num;                     //Sequence number of ESPNOW data.
    uint16_t crc;                         //CRC16 value of ESPNOW data.
    uint32_t magic;                       //Magic number which is used to determine which device to send unicast ESPNOW data.
    //uint8_t payload[0];                   //Real payload of ESPNOW data.
    char payload[0]; 
} __attribute__((packed)) dez_espnow_data_t;

/* Parameters of sending ESPNOW data. */
typedef struct {
    bool unicast;                         //Send unicast ESPNOW data.
    bool broadcast;                       //Send broadcast ESPNOW data.
    uint8_t state;                        //Indicate that if has received broadcast ESPNOW data or not.
    uint32_t magic;                       //Magic number which is used to determine which device to send unicast ESPNOW data.
    uint16_t count;                       //Total count of unicast ESPNOW data to be sent.
    uint16_t delay;                       //Delay between sending two ESPNOW data, unit: ms.
    int len;                              //Length of ESPNOW data to be sent, unit: byte.
    uint8_t *buffer;                      //Buffer pointing to ESPNOW data.
    uint8_t dest_mac[ESP_NOW_ETH_ALEN];   //MAC address of destination device.
} dez_espnow_send_param_t;


#define ESPNOW_MAXDELAY 512

void dez_wifi_init(void);
//void dez_espnow_deinit(dez_espnow_send_param_t *send_param);
void sendVoice(const uint8_t *data, uint16_t len);
esp_err_t dez_espnow_init(void);

#endif